// const i18n = require('i18n');
//
// const Wave = require('wave');
// import { Waves, fishLine, Ease,Wave } from 'fishLine';
// 
const Constants = require('Constants');

let {
	Waves,
	Ease,
	Wave
} = require('WaveBase');

let {
	FishLine
} = require("FishLine");

var FishLineBase = require("FishLineBase");
var hellotemp = require("HelloWorld");

let OnTouchCtrl = cc.Class({
	extends: cc.Component,
	// name: "OnTouchCtrrl",
	properties: {
		canvas: cc.Node,
		touchLocationDisplay: {
			default: null,
			type: cc.Label
		},
		follower: {
			default: null,
			type: cc.Node
		},
		followSpeed: 20,
		speed: 10,
		moveSpeed: 400,
		startPos: cc.p,
		throwFishAce: false,
		isFishing: true,
		curFishID:0,

		waves: {
			default: function() {
				return [new Wave()];
			},
			type: [Wave]
		},

		fishPrefab: {
			default: null,
			type: cc.Prefab
		},
		fishLine: {
			default: null,
			type: FishLine
		},
		// fishLineBase:{
		//     default: function() {
		//         return new FishLineBase();
		//     },
		//     type: FishLineBase

		// }

	},

	initRoomSocket: function(){
		var self = this;
		 G.roomSocket.on('update chessboard', function (chessCoor) {
		 	// cc.log("receive CreatrFish"+ fishID);
            self.fallChess(self.chesses[chessCoor.x][chessCoor.y]);
        });
        G.roomSocket.on('change turn', function () {
            G.gameManager.changeTurn();
        });
        G.roomSocket.on('force change turn', function () {
            G.gameManager.forceChangeTurn();
        });

         G.roomSocket.on('creatfish', function (fishID) {
         	cc.log("receive CreatrFish"+ fishID);
            self.createFish();
        });

	},


	initFishPool: function() {
		var self = this;
		self.fishPool = new cc.NodePool();
		let initCount = 5;
		for (let i = 0; i < initCount; ++i) {
			let enemy = cc.instantiate(self.fishPrefab); // 创建节点
			self.fishPool.put(enemy); // 通过 putInPool 接口放入对象池
		}
	},

	// use this for initialization
	onLoad: function() {
		var self = this;
		
		self.initFishPool();

		self.moveToPos = cc.p(0, 0);
		self.startPos = self.endPos = cc.p(0, 0);
		self.isMoving = false;

		var ctx = self.getComponent(cc.Graphics);
		// var hellotemp = new HelloWorld();
		hellotemp.testLog();
		hellotemp.setText("test");
		// self.fishLine = new FishLine();

		// self.fishLine.initWaveParam(cc.p(200,200), cc.p(200, 410), 200);
		self.fishLine.initWaveWihtGraphicsAndWave(ctx, self.waves);

		self.schedule(self.emitCreateFish, 5);

		self.canvas.on(cc.Node.EventType.TOUCH_START, function(event) {
			var touches = event.getTouches();
			var touchLoc = touches[0].getLocation();
			self.isMoving = true;
			self.startPos = self.node.convertToWorldSpace(touchLoc);
			self.moveToPos = self.node.convertToWorldSpace(touchLoc);
			// cc.log("startPos is %f, %f", self.startPos.x, self.startPos.y);
		}, self.node);

		self.canvas.on(cc.Node.EventType.TOUCH_MOVE, function(event) {
			var touches = event.getTouches();
			var touchLoc = touches[0].getLocation();
			self.moveToPos = self.node.convertToWorldSpace(touchLoc);
			self.endPos = self.moveToPos;
		}, self.node);

		self.canvas.on(cc.Node.EventType.TOUCH_END, function(event) {
			// cc.log("endPos is %f, %f", self.endPos.x, self.endPos.y);
			var direction = cc.pNormalize(cc.pSub(self.endPos, self.startPos));
			if (self.throwFishAce) {
				self.fishLine.takeUpLine(100);
			} else {

				// cc.log("newPos is %f, %f", newPos.x, newPos.y);
				var newPos = cc.pMult(direction, self.moveSpeed);
				newPos = cc.pAdd(cc.p(480, 0), newPos);
				self.fishLine.initWaveParam(cc.p(480, 0), newPos, 200);
				self.throwFishAce = true;
			}

			self.isMoving = false; // when touch ended, stop moving
		}, self.node);

		self.initRoomSocket();
	},

	onDisable: function() {
		if (cc.director.setClearColor) {
			cc.director.setClearColor(cc.Color.BLACK);
		}
	},

	emitCreateFish: function(){
		G.roomSocket.emit('newfish', this.curFishID);
		this.curFishID++;
	},

	createFish: function() {
		var fish = this.fishPool.get();
		if (!fish) {
			fish = cc.instantiate(this.prefab);
		}
		fish.getComponent("FishBase").initStartPos(cc.p(0, 500+Math.ceil(Math.random() * 100) )); 
		this.fishLine.node.addChild(fish);
	},



	// called every frame, uncomment this function to activate update callback
	update: function(dt) {
		// if (this.throwFishAce) {
		//     this.fishLine.update(dt);
		// }

		// if (this.fishLine.isDrawFinished())
		//     this.throwFishAce = false;

	},



});

module.exports = OnTouchCtrl;